//
//  KZHomeVC.swift
//  BoyeStore
//
//  Created by 朱凯 on 2018/6/23.
//  Copyright © 2018年 itboye. All rights reserved.
//

import UIKit

import RxCocoa
import RxSwift
import RxDataSources

class KZHomeVC: KZBaseViewController {
    
    let bag = DisposeBag()
    var collect: UICollectionView! = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "博也商城"
        self.addNavigationItems(items: ["首页-搜索"], position: .right, mode: .image)
        
        setupUI()
    }
    
    override func viewSafeAreaInsetsDidChange() {
        
        if #available(iOS 11.0, *) {
            super.viewSafeAreaInsetsDidChange()
            collect.snp.updateConstraints { (m) in
                m.bottom.equalTo(-view.safeAreaInsets.bottom)
            }
        }
    }
    
    override func rightAction(index: Int) {
        let vc = KZSearchVC()
        self.navigationController?.pushViewController(vc, animated: true)
    }

    
}
extension KZHomeVC {
    
    
    
    private func setupUI() {
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: (view.width - 5 * 3) / 2, height: (view.width - 5 * 3) / 2 + 5 + 40 + 20 + 20)
        layout.headerReferenceSize = CGSize(width: view.width, height: view.width / 2 + 50)
        layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5)
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 5
        
        collect = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.addSubview(collect)
        collect.snp.makeConstraints { (m) in
            m.edges.equalTo(view).inset(UIEdgeInsetsMake(0, 0, 49, 0))
        }
        collect.backgroundColor = kBgColor
        collect.register(KZHomeProductCell.self,
                         forCellWithReuseIdentifier: "cell")
        collect.register(KZHomeCollectHeader.self,
                         forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,
                         withReuseIdentifier: "header")
        
        let items = BehaviorRelay.init(value: [
            SectionModel(model:
                [
                    KZRingBannerItem(),
                    KZRingBannerItem()
                ], items:
                [
                    "1",
                    "2",
                    "3",
                    "4",
                ])
            ])
        
        let dataSource = RxCollectionViewSectionedReloadDataSource
            <SectionModel<[KZRingBannerItem], String>>(
                configureCell: { (dataSource, collectionView, indexPath, element) in
                    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell",
                                                                  for: indexPath) as! KZHomeProductCell
            
                    cell.iconView.image = UIImage.init(named: "首页-测试\(element)")
                    return cell
                    
            },
                configureSupplementaryView: {
                    (dataSource ,cv, kind, ip) in
                    let section = cv.dequeueReusableSupplementaryView(ofKind: kind,
                                                                      withReuseIdentifier: "header", for: ip) as! KZHomeCollectHeader
                    section.banner.items = dataSource.sectionModels.first!.model
                    return section
            }
        )
        items
            .bind(to: collect.rx.items(dataSource: dataSource))
            .disposed(by: bag)
        
        
        collect.rx
            .modelSelected(String.self).subscribe(onNext: {
                
                let vc = KZProductDetailVC()
                self.navigationController?.pushViewController(vc, animated: true)
                
                print($0)
                
//
//                items.accept([SectionModel(model:
//                    [
//                        KZRingBannerItem(),
//                        KZRingBannerItem(),
//                        KZRingBannerItem(),
//
//                        KZRingBannerItem()
//                    ], items:
//                    [
//                        "Swift",
//                        "PHP",
//                        "PHP",
//                        "PHP",
//                        "PHP",
//                        "PHP",
//                        "PHP",
//                        "PHP",
//                        "PHP",
//                        "PHP",
//                        "PHP",
//
//                        ])])
            })
            .disposed(by: bag)

        
       
    }
}

extension KZHomeVC {
    
    
}

